package com.itboye.textileexpres.login;

import java.util.Observable;
import java.util.Observer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
import com.amap.api.location.AMapLocationListener;
import com.itboye.textileexpes.beans.UserBean;
import com.itboye.textileexpes.presenter.MainPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.base.SpPublicParams;
import com.itboye.textileexpres.logincontroller.LoginController;
import com.itboye.textileexpres.logincontroller.LoginedState;
import com.itboye.textileexpres.util.IsMobileNumPhone;
import com.itboye.textileexpres.util.SPContants;
import com.itboye.textileexpres.util.SPUtils;
import com.itboye.textileexpres.util.Utils;
import com.itboye.textileexpres.widget.RoundImageView;
import com.itboye.textileexptes.config.GlobalConfig;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class LoginActivity extends BaseActivity implements OnClickListener,
		Observer {
	RoundImageView logo_img;
	EditText phone_num, pass_word;
	TextView register_tv, VDword_login, login_tv;
	ImageView WX;
	private static final String APP_ID = "";//
	private IWXAPI api;
	String phone;
	String password;
	MainPresenter mainPresenter;
	ImageView back;
	private RelativeLayout country_rel;
	private String countryName;
	private TextView country_code;
	private TextView country_tv;
	String number;

	@Override
	public int initView() {
		// TODO Auto-generated method stub u
		return R.layout.activity_login;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		api = WXAPIFactory.createWXAPI(this, APP_ID, false);
		phone_num.setText("13484379290");
		pass_word.setText("123456");
		mainPresenter = new MainPresenter(this);
		onProvince();
	}

	public void onProvince() {
		if (number == null || countryName == null) {
			number = "+86";
			countryName = "中国";
		}
		country_code.setText(number);
		country_tv.setText(countryName);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent = null;
		switch (v.getId()) {
		case R.id.register_tv:
			intent = new Intent(LoginActivity.this, SMSReloginActivity.class);
			intent.putExtra("name", countryName);
			intent.putExtra("number", number);
			startActivity(intent);
			// intent = new Intent(LoginActivity.this, RegisterActivity.class);
			// startActivity(intent);
			break;
		case R.id.VDword_login:
			intent = new Intent(LoginActivity.this, SMSSendActivity.class);
			intent.putExtra("name", countryName);
			intent.putExtra("number", number);
			startActivity(intent);
			finish();
			break;
		case R.id.login_tv:
			onLogin(number);
			break;
		case R.id.WX:
			final SendAuth.Req req = new SendAuth.Req();
			req.scope = "snsapi_userinfo";
			req.state = "none";
			req.transaction = "login";
			api.sendReq(req);
			break;
		case R.id.back:
			finish();
			break;
		case R.id.country_rel:
			intent = new Intent();
			intent.setClass(LoginActivity.this, RegionActivity.class);
			// s intent.putExtra("cityId", cityId);
			startActivityForResult(intent, 101);
			break;
		default:
			break;
		}
	}

	private void onLogin(String number) {
		// TODO Auto-generated method stub
		phone = phone_num.getText().toString();
		password = pass_word.getText().toString();
		if (IsMobileNumPhone.isMobileNum(phone) == false) {
			toast("请输入正确手机号");
			return;
		} else if (password.equals("") || password == null) {
			toast("密码不正确");
			return;
		} else {
			if (number == null || number.equals("+86")) {
				number = "+86";
			}
			mainPresenter.login(phone, password, number + "");
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = super.handlerError(data);
		if (entity != null) {
			if (entity.getEventType().equals(MainPresenter.LoginSuccessEvent)) {
				// toast(entity.getData() + "");
				LoginSuccess(entity);

			} else {
				toast(entity.getData() + "");
			}
		}
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		// TODO Auto-generated method stub
		super.onActivityResult(arg0, arg1, arg2);
		if (arg0 == 101 && arg1 == 104) {
			String name = arg2.getStringExtra("province");
			number = arg2.getStringExtra("number");
			country_code.setText(number);
			country_tv.setText(name);
		}

	}

	private void LoginSuccess(ResultEntity entity) {
		LoginController.setLoginState(new LoginedState());

		Intent intent2 = new Intent("newshead");
		sendBroadcast(intent2);
		Intent intent = new Intent("LoginSuccess");
		sendBroadcast(intent);
		UserBean bean = (UserBean) entity.getData();
		SPUtils.put(this, null, SpPublicParams.UID, bean.getUid() + "");
		SPUtils.put(this, null, SpPublicParams.USER_NAME, bean.getUsername()
				+ "");
		SPUtils.put(this, null, SpPublicParams.PASSWORD, bean.getPassword()
				+ "");
		SPUtils.put(this, null, SpPublicParams.NICK_NAME, bean.getNickname()
				+ "");
		SPUtils.put(this, null, SpPublicParams.PHONE_MODEL, bean.getMobile()
				+ "");
		SPUtils.put(this, null, SpPublicParams.realname, bean.getRealname()
				+ "");
		SPUtils.put(this, null, SpPublicParams.USERHEAD, bean.getHead());
		SPUtils.put(this, null, SpPublicParams.Email, bean.getEmail());
		SPUtils.put(this, null, SpPublicParams.SIGNATURE, bean.getSign());
		SPUtils.put(this, null, SpPublicParams.SEX, bean.getSex() + "");
		SPUtils.put(this, null, SpPublicParams.Company, bean.getCompany() + "");
		SPUtils.put(this, null, SpPublicParams.Job, bean.getJobTitle() + "");
		SPUtils.put(this, null, SpPublicParams.AcceptAddress,
				bean.getDefaultAddress() + "");
		SPUtils.put(this, null, SpPublicParams.USERHEAD, bean.getHead() + "");
		SPUtils.put(this, null, SpPublicParams.WX, bean.getWeixin() + "");
		SPUtils.put(this, null, SpPublicParams.IS_LOGINED, "1");
		SPUtils.put(this, null, SpPublicParams.Auto_Code,
				bean.getAutoLoginCode() + "");
		String time = String.valueOf((int) (Math.random() * 10));
		SPUtils.put(this, null, SpPublicParams.URL, GlobalConfig.IMAGE_HEAD
				+ bean.getUid() + "&&random" + time);
		Intent intentt = new Intent("LoginSuccess");
		sendBroadcast(intentt);
		finish();
	}

}
